﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Controls
{
    public partial class BoxRegister : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                captchaImage.ImageUrl = new CaptchaProvider().CreateCaptcha();
                txtUsername.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtConPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtEmail1.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtName.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtCompany.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtAddress.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtDienThoai.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtDiDong.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");
                txtCaptcha.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSubmit.ClientID + "')");

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int nextid = Convert.ToInt32(DAL.DCollector.DBCollector.Instance.getNextID("tbl_Employee", "Employee_ID"));
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            string conpassword = txtConPassword.Text;
            string email = txtEmail1.Text;
            string fullname = txtName.Text;
            string birthday = ddlYear.SelectedValue + "-" + ddlMonth.SelectedValue + "-" + ddlDay.SelectedValue;
            string sex = ddlSex.SelectedValue;
            string dienthoai = txtDienThoai.Text;
            string didong = txtDiDong.Text;
            string address = txtAddress.Text;
            string company = txtCompany.Text;

            string password_mahoa = LIB.Converter.Encode("sha1", password);
            CaptchaProvider capchaPro = new CaptchaProvider();
            DataTable checkuseremail = DAL.DCollector.DBCollector.Instance.StoredProcedureCheckUser(username, email);
            if (checkuseremail.Rows[0]["result"].ToString() == "0")
            {
                if (capchaPro.IsValidCode(txtCaptcha.Text))
                {

                    int idnextemployee = nextid + 1;
                    string value = "" + idnextemployee + ",N'" + username + "','" + password_mahoa + "','" + email + "',N'" + fullname + "','" + birthday + "',N'" + sex + "','" + dienthoai + "','" + didong + "',N'" + address + "','" + company + "','/Images/Image-User/default.jpg','1'";
                    List<string> values = new List<string>();
                    values.Add(value);
                    DAL.DCollector.DBCollector.Instance.Insert("tbl_Employee", values, false);
                    Session["Username"] = txtUsername.Text;
                    Session["Employee_FullName"] = fullname;
                    Response.Redirect("~/thanhcong");
                }
                else
                {
                    lblCheckCaptcha.Text = "Mã bảo mật không đúng";
                }
            }
            else
            {
                lblCheckUserAndEmail.Text = "Tên tài khoản hoặc Email đã tồn tại";
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtConPassword.Text = "";
            txtEmail1.Text = "";
            txtName.Text = "";
            txtCompany.Text = "";
            txtAddress.Text = "";
            txtDienThoai.Text = "";
            txtDiDong.Text = "";
            txtCaptcha.Text = "";
        }
    }
}